#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    connect(ui->push_button_01, &QPushButton::clicked, this, [=]() {
        QProgressDialog *progress_dialog = new QProgressDialog("Copying Data...", "Cancel Copy", 0, 100, this);
        progress_dialog->setWindowTitle("Copy Data");
        progress_dialog->setFixedSize({250, 150});
        // 模态显示窗口对话框
        // progress_dialog->setWindowModality(Qt::WindowModal);
        progress_dialog->show();

        static int count = 0;
        QTimer *timer = new QTimer(this);
        timer->start(10);

        connect(timer, &QTimer::timeout, this, [=]() {
            progress_dialog->setValue(count);
            count++;

            if (count > progress_dialog->maximum()) {
                timer->stop();
                count = 0;

                delete timer, progress_dialog;
            }
        });

        connect(progress_dialog, &QProgressDialog::canceled, this, [=]() {
            timer->stop();
            count = 0;

            delete timer, progress_dialog;
        });

    });
}

Widget::~Widget()
{
    delete ui;
}

